var express = require('express');
var router = express.Router();

let mongoose = require('mongoose');

//在要解密的后端代码模块中引入jsonwebtoken
let jsonwebtoken = require('jsonwebtoken')
require('../util/authConfig')



router.post('/getUser',async function(req, res) {
  //获取请求头中的token数据
  let auth = req.header('Authorization');
  let token = auth.replace('Bearer ','');
  //将token进行解密
  let userObj = jsonwebtoken.verify(token,'star').user;
    // 数据样式：userObj={
    // exp:'设置鉴权时间',
    // iat:'鉴权结束时间',
    // user:{account:'admin', ·
          // password:'',
          // _id:''
    //  }
//  }
  //根据解密token查找数据库对应的条目，响应给前端
  let re =await mongoose.model('studentsModel').find({
    _id:userObj._id
  })
  res.send({
    code:1,
    message:'获取学生信息成功！',
    data:re,
  });
});


router.post('/upData',async function(req, res) {

  let id = req.body.id;
  let name = req.body.name;
  let gender = req.body.gender;

  await mongoose.model('studentsModel').updateMany({_id:id},{name:name,gender:gender})

  let re = await mongoose.model('studentsModel').find({_id:id})
  res.send({
    code:1,
    message:'获取学生信息成功！',
    data:re
  });
});

module.exports = router;
